﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LGPSDataGen.Generators
{
    class WindGenerator : GenericWeatherGenerator
    {
        //Nothing special, just call base Generic weather generator
        public WindGenerator(DateTime mDate)
            : base(mDate)
        {
            
        }
        /// <summary>
        /// First line is blank
        /// Second line is header
        /// HH:MM:SS AM, Temperature
        /// </summary>
        public override void ProcessData()
        {
            dataArray = new List<string>();

            StreamReader fileToOpen = new StreamReader(pathToFile);
            SkipTopTwoLines(fileToOpen);

            string currentLine = fileToOpen.ReadLine();

            int prevHour = -1;
            string fullDate = "";
            int lastHour = 0;
            double lastValue = 0;
            DateTime lastDate = new DateTime();
            while (!fileToOpen.EndOfStream && !currentLine.Contains("<!--"))
            {
                string[] splitCurrentLine = currentLine.Split(',');
                int hour = int.Parse(splitCurrentLine[0].Split(':')[0]);
                if (splitCurrentLine[0].Contains("PM") && splitCurrentLine[0].StartsWith("12"))
                    hour = 12;
                else if (splitCurrentLine[0].Contains("PM"))
                    hour = hour + 12;
                else if (splitCurrentLine[0].StartsWith("12"))
                    hour = 0;
                
                if (prevHour != hour)
                {
                    bool addDefaultRow = false;
                    int hoursToDo = 0;
                    if (prevHour + 1 != hour)
                    {
                        hoursToDo = hour - prevHour - 1;
                        addDefaultRow = true;
                    }
                    prevHour = hour;
                    string x = splitCurrentLine[7];
                    double windSpeed = 0;

                    if (x.Equals("Calm"))
                        windSpeed = 0;
                    else
                        windSpeed = double.Parse(x);

                    string[] s = pathToFile.Split('_');
                    int year = int.Parse(s[1]);
                    int month = int.Parse(s[2]);
                    int day = int.Parse(s[3]);
                    fullDate = year + "_" + month + "_" + day;
                    DateTime d = new DateTime(year, month, day);
                    if (addDefaultRow) //add default data if data source skipped an hour
                    {
                        while (hoursToDo > 0)
                        {
                            if (hour - hoursToDo >= 0)
                            {
                                StoreToDB(d, hour - hoursToDo, windSpeed);
                            }
                            hoursToDo--;
                        }
                    }
                    StoreToDB(d, hour, windSpeed);
                    lastDate = d;
                    lastHour = hour;
                    lastValue = windSpeed;
                }
                currentLine = fileToOpen.ReadLine();
                AdvanceToNextHour(fileToOpen);
            }
            while (lastHour != 23)
            {
                lastHour++;
                StoreToDB(lastDate, lastHour, lastValue);
            }
            File.WriteAllLines(@"Wind-" + fullDate + ".csv", dataArray.ToArray());
            fileToOpen.Close();
        }

        public void StoreToDB(DateTime date, int hour, double value)
        {
            try
            {
                date = date.AddHours(hour);
                int x = (int)value;
                string data = hour.ToString() + "," + value.ToString();
                dataArray.Add(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                return;
            }
        }
        
        private List<string> dataArray;
    }
}
